-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow mocking of ARTPaginatedResult #1986
Conversation
Note Reviews pausedUse the following commands to manage reviews:
WalkthroughThe changes in this pull request involve modifications to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
9cf4125
to
66accb2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caution
Inline review comments failed to post
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (2)
Source/include/Ably/ARTPaginatedResult.h (1)
30-31
: Approved: Enables mocking as intended, but consider additional safeguardsThe changes to the
init
method and its associated comment effectively achieve the PR's objective of allowing mocking ofARTPaginatedResult
. This improvement in testability is valuable for the project.However, there's a potential risk of misuse if developers instantiate
ARTPaginatedResult
directly without subclassing. To mitigate this:
- Consider adding a compile-time warning or runtime assertion in the
init
method to further discourage direct usage.- Update the class documentation to clearly state that this class is intended for subclassing, not direct instantiation.
Would you like assistance in implementing these additional safeguards?
Source/ARTPaginatedResult.m (1)
35-41
: Consider markinginit
as a designated initializerSince
init
is now publicly available and sets_initializedViaInit
, consider explicitly marking it as a designated initializer. This enhances clarity and ensures proper initialization in subclasses.
🛑 Comments failed to post (1)
Source/ARTPaginatedResult.m (1)
88-116: 🛠️ Refactor suggestion
Refactor repeated exception handling into a helper method
The checks for
_initializedViaInit
and the associated exceptions are repeated in the methodshasNext
,isLast
,items
,first
, andnext
. To improve maintainability and reduce code duplication, consider extracting this logic into a helper method.Apply this diff to create a helper method and utilize it in the relevant methods:
+ - (void)assertInitializedViaSubclass { + if (_initializedViaInit) { + [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; + } + } - (BOOL)hasNext { - if (_initializedViaInit) { - [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; - } + [self assertInitializedViaSubclass]; return _hasNext; } - (BOOL)isLast { - if (_initializedViaInit) { - [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; - } + [self assertInitializedViaSubclass]; return _isLast; } - (NSArray<id> *)items { - if (_initializedViaInit) { - [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; - } + [self assertInitializedViaSubclass]; return _items; } - (void)first:(void (^)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error))callback { - if (_initializedViaInit) { - [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; - } + [self assertInitializedViaSubclass]; // ... rest of the method ... } - (void)next:(void (^)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error))callback { - if (_initializedViaInit) { - [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; - } + [self assertInitializedViaSubclass]; // ... rest of the method ... }Also applies to: 130-133
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🧹 Outside diff range and nitpick comments (1)
Source/include/Ably/ARTPaginatedResult.h (1)
Line range hint
1-14
: Update class documentation to reflect new initialization behaviorWith the changes to the
init
method, it's important to update the class documentation to provide clear guidance on usage, especially regarding subclassing and thread safety. Consider adding the following information to the class documentation:
- Explain that this class is designed for subclassing when used in production code.
- Clarify the intended use of the public initializer for mocking in test scenarios.
- Provide guidance on how to properly subclass
ARTPaginatedResult
for production use.- Ensure that the
NS_SWIFT_SENDABLE
attribute is still appropriate with the new initialization behavior, and document any thread-safety considerations.Here's a suggested addition to the class documentation:
/** * Contains a page of results for message or presence history, stats, or REST presence requests. An `ARTPaginatedResult` response from a REST API paginated query is also accompanied by metadata that indicates the relative queries available to the `ARTPaginatedResult` object. * * This class is designed to be subclassed for production use. The public initializer is provided primarily for creating mock instances in test scenarios. When subclassing, ensure to override and implement all required methods and properties. * * Thread Safety: This class is marked as `NS_SWIFT_SENDABLE`. Ensure that any subclass maintains thread safety, especially when overriding methods or properties. */This addition provides crucial information for developers working with this class, reducing the likelihood of misuse and clarifying its intended usage patterns.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- Source/ARTPaginatedResult.m (5 hunks)
- Source/include/Ably/ARTPaginatedResult.h (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
Source/include/Ably/ARTPaginatedResult.h (1)
Line range hint
1-46
: Summary and Final RecommendationThe changes made to
ARTPaginatedResult.h
successfully enable mocking of theARTPaginatedResult
class, meeting the primary objective of the PR. However, there are several areas where the implementation could be improved:
- The initialization approach could be enhanced for better clarity and safety.
- Class documentation should be updated to reflect the new initialization behavior and provide guidance on proper usage.
- Consider the implications of these changes on thread safety and the
NS_SWIFT_SENDABLE
attribute.While the current implementation is functional, implementing the suggested improvements would result in a more robust, clear, and maintainable API. These changes would benefit both the developers working on the Ably SDK and those using it in their applications.
Recommendation: Approve with comments. The PR meets its objective, but consider implementing the suggested improvements before merging.
66accb2
to
61d9c11
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
Source/ARTPaginatedResult.m (1)
87-88
: Use appropriate exception type and include method name in exception messageThe method
initializedViaInitCheck
currently raises anNSInvalidArgumentException
with a fixed message. It's more appropriate to useNSInternalInconsistencyException
, and including the method name in the exception message will provide more clarity about which method needs to be overridden.Apply this diff to improve the exception:
- [NSException raise:NSInvalidArgumentException format:@"When initializing this class using -init, you need to override this method in a subclass"]; + [NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass when initializing this class using -init", NSStringFromSelector(_cmd)];
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- Source/ARTPaginatedResult.m (5 hunks)
- Source/include/Ably/ARTPaginatedResult.h (1 hunks)
🧰 Additional context used
@umair-ably please can you change it so that I am not listed as the commit's sole author? It gives the impression that I wrote all of the code that's in here, which I didn't (e.g. |
This commit message isn't quite right. The base class remains responsible for initialising its own properties. A more accurate description would be "by allowing it to be subclassed by a user of the SDK". |
61d9c11
to
ca4e4cb
Compare
@coderabbitai pause |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
Source/include/Ably/ARTPaginatedResult.h (1)
29-30
: Good improvements, consider additional enhancements for clarity and safetyThe changes align well with the PR objective of enabling mocking for
ARTPaginatedResult
. The added documentation clearly explains the limitations and intended use, and the use ofNS_DESIGNATED_INITIALIZER
is a good improvement. However, to further enhance the API's clarity and safety, consider the following suggestions:
- Use the
NS_SWIFT_UNAVAILABLE
macro to prevent unintended usage in Swift:- (instancetype)init NS_DESIGNATED_INITIALIZER NS_SWIFT_UNAVAILABLE("Use a subclass for creating mock instances in tests");
- Add a class method for creating mock instances, making the testing purpose more explicit:
+ (instancetype)mockForTesting;
These changes would provide a clearer API for both production and testing use cases while maintaining the desired flexibility for mocking and adding an extra layer of safety.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- Source/ARTPaginatedResult.m (5 hunks)
- Source/include/Ably/ARTPaginatedResult.h (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- Source/ARTPaginatedResult.m
🧰 Additional context used
✅ Actions performedReviews paused. |
…y a user of the SDK Co-authored-by: Lawrence Forooghian <[email protected]>
ca4e4cb
to
0c91f1b
Compare
@coderabbitai pause |
✅ Actions performedReviews paused. |
Allow mocking of ARTPaginatedResult by passing responsibility of property initialisation to any subclassing parties
Summary by CodeRabbit
New Features
ARTPaginatedResult
class, allowing for better control over object instantiation.hasNext
,isLast
, anditems
).Bug Fixes
Documentation
init
method to clarify its intended use and restrictions for subclasses.